<template>
  <div class="search-container" v-loading="loading">
    <el-form v-model="searchPage" inline="true" >
      <el-form-item  v-for="(item, index) in searchPage.items" :key="index" :label="item.cnname">
        <el-select  v-if="item.itemType === 'DICT'" v-model="item.value" :multiple = "item.mutSelect ==='checkBox'">
          <el-option  v-for="(itemChild, itemIndex) in item.dict" :key="itemIndex"  :label="itemChild.value" :value="itemChild.key" />
        </el-select>
        <el-input v-else-if="item.itemType !== 'DATE'" v-model="item.value"></el-input>
        <el-col :span="24" v-else>
          <el-form-item>
            <el-date-picker
              v-model="item.dateValue"
              type="date"
              label="Pick a date"
              placeholder="Pick a date"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button type="primary" @click="onCreate">新建</el-button>
        <el-button @click="onImport">导入</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="data-container">
      <el-table :data="tableData" style="width: 100%">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" width="80" label="序号"/>
      <el-table-column prop="createTime" label="创建时间" width="160" />
      <el-table-column prop="jobUnit" label="招聘单位" width="120" />
      <el-table-column prop="mainDepartment" label="主管部门" width="120" />
      <el-table-column prop="jobType" label="岗位类别" width="120" />
      <el-table-column prop="code" label="岗位编码" width="120" />
      <el-table-column prop="name" label="岗位名称" width="120" />
      <el-table-column prop="nums" label="招聘人数" width="60" />
      <!--<el-table-column prop="described" label="描述" width="280" />-->
      <el-table-column prop="maxAge" label="最高年龄" width="150"></el-table-column>
      <el-table-column prop="sexLimit" label="性别" width="120" />
      <el-table-column prop="lowBachelor" label="最低学历" width="120" />
      <el-table-column prop="lowDegree" label="最低学位" width="120" />
      <el-table-column prop="profession" label="所学专业" width="180" />
      <!--<el-table-column prop="department" label="科室" width="120" />-->
      <!--<el-table-column prop="personType" label="人员类别" width="120"></el-table-column>-->
      <el-table-column prop="otherCondition" label="其他条件" width="120" />
      <el-table-column prop="publishDate" label="发布日期" width="160" />
      <el-table-column prop="endDate" label="截至日期" width="160" />
      <el-table-column prop="publish" label="是否发布" width="160">
        <template #default="scope">
          <el-select v-model="scope.row.publish" placeholder="" name="publish" disabled >
            <el-option label="是" value="true" />
            <el-option label="否" value="false" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="140">
        <!-- vue3中 solt-scop已经放弃使用 -->
        <template #default="scope">
           <el-button text type="danger" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
           <el-button text type="danger" size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
     <el-pagination
      v-model:currentPage="searchPage.current"
      v-model:page-size="searchPage.pageSize"
      :page-sizes="[20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableDatas.total"
      hide-on-single-page=true
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <div>
    <import-excel v-model="modelValue" :uploadUrl="uploadUrl"></import-excel>
  </div>
  <div>
    <!--<edit-form v-model="showEditModel" :schedule="currentSchedule"></edit-form>-->
    <el-dialog
      v-model="dialogVisible"
      title="编辑"
      width="60%"
    >
      <el-form :model="currentJob" ref="currentJobRef" label-width="360px" label-position="top">
        <el-form-item label="id" v-if="false" prop="id">
          <el-input v-model="currentJob.id" name="id" />
        </el-form-item>
        <el-form-item label="招聘单位" prop="jobUnit">
          <el-input v-model="currentJob.jobUnit" name="jobUnit" />
        </el-form-item>
        <el-form-item label="主管部门" prop="mainDepartment">
          <el-input v-model="currentJob.mainDepartment" name="mainDepartment" />
        </el-form-item>
        <el-form-item label="岗位类别" prop="jobType">
          <el-input v-model="currentJob.jobType" name="jobType"/>
        </el-form-item>
        <el-form-item label="岗位编码" prop="code">
          <el-input v-model="currentJob.code" name="code" />
        </el-form-item>
        <el-form-item label="岗位名称" prop="name">
          <el-input v-model="currentJob.name" name="name" />
        </el-form-item>
        <el-form-item label="招聘人数" prop="nums">
          <el-input v-model.number="currentJob.nums" name="nums"/>
        </el-form-item>
        <el-form-item label="最大年龄" prop="maxAge">
          <el-input type="number"  v-model.number="currentJob.maxAge" name="maxAge"/>
        </el-form-item>
        <el-form-item label="性别" prop="sexLimit">
          <el-input v-model="currentJob.sexLimit" name="sexLimit"/>
        </el-form-item>
        <el-form-item label="最低学历" prop="lowBachelor">
          <el-input v-model="currentJob.lowBachelor"  name="lowBachelor"/>
        </el-form-item>
        <el-form-item label="最低学位" prop="lowDegree">
          <el-input v-model="currentJob.lowDegree"  name="lowDegree"/>
        </el-form-item>
        <el-form-item label="所学专业" prop="profession">
          <el-input v-model="currentJob.profession"  name="profession"/>
        </el-form-item>
        <el-form-item label="其他条件" prop="otherCondition">
          <el-input type="textarea" v-model="currentJob.otherCondition"  name="otherCondition"/>
        </el-form-item>
        <el-form-item label="发布日期" required>
          <el-col :span="12">
            <el-form-item prop="publishDate">
              <el-date-picker
                v-model="currentJob.publishDate"
                type="date"
                placeholder=""
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="截止日期">
          <el-col :span="12">
            <el-form-item prop="endDate">
              <el-date-picker
                v-model="currentJob.endDate"
                type="date"
                placeholder=""
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSave()">保存</el-button>
          <!-- <el-button @click="onReset">{{$t('msg.common.reset')}}</el-button> -->
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onActivated } from 'vue'
import { ElMessage } from 'element-plus'
import { list, deleteById, downLoadFile, add, modify } from '@/api/common/common'
import { useI18n } from 'vue-i18n'
import { dateToString, dateToNext, dateTimeToString, dToS } from '@/utils/date'
import ImportExcel from '@/components/ImportExcel.vue'

const tableDatas = ref({})

const tableData = ref([])

const currentJob = ref({})

const dialogVisible = ref(false)

const modelValue = ref(false)

const uploadUrl = process.env.VUE_APP_BASE_API + '/job/import'

const onImport = () => {
  modelValue.value = true
}

const i18n = useI18n()

const searchPage = ref({
  items: [
    {
      name: 'name',
      cnname: '名称',
      itemType: 'STRING',
      sign: 'like',
      value: '',
      dateValue: ''
    },
    {
      name: 'described',
      cnname: '描述',
      itemType: 'STRING',
      sign: 'like',
      value: '',
      dateValue: ''
    },
    {
      name: 'createTime1',
      cnname: '开始时间',
      itemType: 'DATE',
      sign: '>=',
      value: dateToString(-7),
      dateValue: dateToNext(-7)
    },
    {
      name: 'createTime2',
      cnname: '结束时间',
      itemType: 'DATE',
      sign: '<=',
      value: dateToString(0),
      dateValue: dateToNext(0)
    }
  ],
  pageSize: 20,
  current: 1,
  sortBy: ['id']
})

const searchFunction = async () => {
  list(searchPage.value, 'job').then(res => {
    tableData.value = res.content
    tableDatas.value.total = parseInt(res.totalElements)
    ElMessage.success('成功')
  }).catch(err => {
    ElMessage.success('失败' || err.message)
  })
}

onActivated(searchFunction())

const onSubmit = () => {
  searchFunction()
}

const onReset = () => {
  searchPage.value = {
    items: [
      {
        name: 'name',
        cnname: '名称',
        itemType: 'STRING',
        sign: 'like',
        value: '',
        dateValue: ''
      },
      {
        name: 'described',
        cnname: '描述',
        itemType: 'STRING',
        sign: 'like',
        value: '',
        dateValue: ''
      },
      {
        name: 'createTime1',
        cnname: '开始时间',
        itemType: 'DATE',
        sign: '>=',
        value: dateToString(-7),
        dateValue: dateToNext(-7)
      },
      {
        name: 'createTime2',
        cnname: '结束时间',
        itemType: 'DATE',
        sign: '<=',
        value: dateToString(0),
        dateValue: dateToNext(0)
      }
    ],
    pageSize: 20,
    current: 1,
    sortBy: ['id']
  }
  // 处理查询
  searchFunction()
}

const handleSizeChange = pageSize => {
  searchPage.value.pageSize = pageSize
  // 执行查询操作
  searchFunction()
}

const handleCurrentChange = current => {
  searchPage.value.current = current
  // 执行查询操作
  searchFunction()
}

const onCreate = () => {
  currentJob.value = {}
  dialogVisible.value = true
  ElMessage.success('新建')
}

const handleDelete = id => {
  deleteById(id, 'job').then(res => {
    ElMessage.warning('删除成功')
    // 重新刷新
    searchFunction()
  })
}

const handleEdit = currentId => {
  dialogVisible.value = true
  currentJob.value = tableData.value.filter(job => {
    return job.id === currentId
  })[0]
  ElMessage.success('编辑')
}

// 保存
const onSave = () => {
  if (currentJob.value.id) {
    modify(currentJob.value, currentJob.value.id, 'job').then(res => {
      ElMessage.success('修改成功')
      dialogVisible.value = false
      currentJob.value = {}
    })
  } else {
    console.log(currentJob)
    add(currentJob.value, 'job').then(res => {
      ElMessage.success('新建成功')
      dialogVisible.value = false
      currentJob.value = {}
    })
  }
}
</script>

<style lang="scss" scoped>
.search-container {
  width: 100%;
  height: 100px;
}
</style>
